﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Framework.Exceptions;
using Interfaces.BusinessLogic;
using Interfaces.Enums;
using Interfaces.Models;
using Interfaces.Repositories;
using Interfaces.RepositoryModels.InternalModels.UserRoleModels;

namespace BusinessLogic.Services.Operations
{
    public class RoleOperationService : IRoleOperationService
    {
        private readonly IRoleRepository _roleRepository;
        
        public RoleOperationService(IRoleRepository roleRepository)
        {
            _roleRepository = roleRepository;
        }

        public IRole GetById(Guid id)
        {
            return
                ExceptionHandlingManager.Process(
                    () => Mapper.DynamicMap<RoleModel, IRole>(_roleRepository.GetById(id)), PolicyNames.DALPolicy);
        }

        public IRole GetByType(RoleType roleType)
        {
            return
               ExceptionHandlingManager.Process(
                   () => Mapper.DynamicMap<RoleModel, IRole>(_roleRepository.GetByType(roleType)), PolicyNames.DALPolicy);
        }

        public IRole GetByType(string roleType)
        {
            var type = (RoleType) Enum.Parse(typeof (RoleType), roleType);
            return Mapper.DynamicMap<RoleModel, IRole>(_roleRepository.GetByType(type));
        }

        public IEnumerable<IRole> GetAll()
        {
            return
             ExceptionHandlingManager.Process(
                 () => from x in _roleRepository.GetAll() select Mapper.DynamicMap<RoleModel, IRole>(x), PolicyNames.DALPolicy);
        }
    }
}
